package geobeans.cn.demo;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;

import cn.geobeans.android.MapActivity;
import cn.geobeans.android.MapView;
import cn.geobeans.app.location.Compass;
import cn.geobeans.backend.canvas.Bitmap;
import cn.geobeans.common.GeoPoint;
import cn.geobeans.common.MapPosition;
import cn.geobeans.common.MercatorProjection;
import cn.geobeans.layers.GCircleOverlay;
import cn.geobeans.layers.ItemizedLayer;
import cn.geobeans.layers.MarkerItem;
import cn.geobeans.layers.MarkerSymbol;
import cn.geobeans.layers.OnItemGestureListener;
import cn.geobeans.layers.tile.bitmap.BitmapTileLayer;
import cn.geobeans.map.Map;
import cn.geobeans.map.MapType;
import cn.geobeans.osmdroid.location.POI;
import cn.geobeans.osmdroid.overlays.DefaultInfoWindow;
import cn.geobeans.osmdroid.overlays.ExtendedMarkerItem;
import cn.geobeans.osmdroid.overlays.ItemizedOverlayWithBubble;
import cn.geobeans.tiling.TileSource;
import cn.geobeans.tiling.source.bitmap.GBitmapTileFactory;

import static cn.geobeans.android.canvas.AndroidGraphics.drawableToBitmap;

public class MainActivity extends MapActivity implements OnItemGestureListener, OnClickListener {


    MapView mMapView;
    Button caiji_point, caiji_line, caiji_poly, end_draw, caiji_rect, caiji_bubmark, clear_all, my_loc;
    ItemizedOverlayWithBubble poiMarkers;


    Compass mCompass;
    PGISEventHandler handler;
    TileSource mTileSource;
    BitmapTileLayer mLayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        initView();
        initMap();
        initEvent();
    }

    private void initMap() {
        registerMapView(mMapView);
        MercatorProjection.setType(MapType.QUADSERVER);
        mTileSource = GBitmapTileFactory.createBMTileSource("http://59.108.36.106/QuadServer/maprequest?services=B_vector", 9, 19);
        mLayer = new BitmapTileLayer(mMap, mTileSource);
        mMap.layers().add(mLayer);


        mMap.setMapPosition(39.896116, 116.434065, Math.pow(2, 15));


        handler = new PGISEventHandler(mMap, this);
        mMapView.initGestureHandler(this, handler);
        mMapView.setDoubleTapListener(handler);
    }

    private void initView() {
        mMapView = (MapView) findViewById(R.id.mapView);
        caiji_point = (Button) findViewById(R.id.caiji_point);
        caiji_line = (Button) findViewById(R.id.caiji_line);
        caiji_poly = (Button) findViewById(R.id.caiji_poly);
        end_draw = (Button) findViewById(R.id.end_draw);
        caiji_rect = (Button) findViewById(R.id.caiji_rect);
        caiji_bubmark = (Button) findViewById(R.id.caiji_bubmark);
        clear_all = (Button) findViewById(R.id.clear_all);
        my_loc = (Button) findViewById(R.id.my_loc);
    }

    private void initEvent() {
        caiji_point.setOnClickListener(this);
        caiji_line.setOnClickListener(this);
        caiji_poly.setOnClickListener(this);
        end_draw.setOnClickListener(this);
        caiji_rect.setOnClickListener(this);
        caiji_bubmark.setOnClickListener(this);
        clear_all.setOnClickListener(this);
        my_loc.setOnClickListener(this);
    }

    int type = -1;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_loc:
                myLoc();
                break;
            case R.id.caiji_point:
                handler.setCaijiOperation(1);
                type = 2;
                break;
            case R.id.caiji_line:
                type = 2;
                handler.setCaijiOperation(2);
                break;
            case R.id.caiji_poly:
                type = 3;
                handler.setCaijiOperation(3);
                break;
            case R.id.end_draw:
                if (type == 3) {
                    handler.polygonClosed();
                }
                handler.setCaijiOperation(-1);
                break;
            case R.id.caiji_rect:
                type = 4;
                handler.setCaijiOperation(4);
                break;
            case R.id.caiji_bubmark:
                addBubmark();
                break;
            case R.id.clear_all:
                handler.clear();
                if (poiMarkers != null)
                    mMap.layers().remove(poiMarkers);
                poiMarkers = null;
                poiItems.clear();
                mMap.updateMap(true);
                break;


        }
    }


    final ArrayList<ExtendedMarkerItem> poiItems = new ArrayList<ExtendedMarkerItem>();

    private void addBubmark() {
        poiMarkers = new ItemizedOverlayWithBubble(mMap, this, null, poiItems, new POIInfoWindow(mMap));
        mMap.layers().add(poiMarkers);
        for (int i = 1; i < 4; i++) {
            POI poi = new POI(POI.POI_SERVICE_NOMINATIM);
            poi.id = "_id_" + i;
            poi.category = "name_" + i;
            poi.type = "type";
            poi.description = "desssss" + i;
            poi.thumbnailPath = "";
            //39.896116, 116.434065
            double lat = 39.896116 + i * 0.01;
            double lon = 116.434065 + i * 0.01;
            poi.location = new GeoPoint(lat, lon);
            ExtendedMarkerItem poiMarker = new ExtendedMarkerItem("中国", "详情" + i, poi.location);
            Bitmap bitmap = drawableToBitmap(getResources(), R.drawable.marker_poi);
            MarkerSymbol marker = new MarkerSymbol(bitmap, 0.5f, 0.5f);
            poiMarker.setMarker(marker);
            poiMarker.setRelatedObject(poi);
            poiMarkers.addItem(poiMarker);
        }
        mMap.render();
        mMap.updateMap
                (true);

    }


    ItemizedLayer markerLayer;

    public void myLoc() {
        if (markerLayer == null) {
            Bitmap bitmap = drawableToBitmap(getResources(), R.mipmap.position);
            MarkerSymbol symbol = new MarkerSymbol(bitmap, 0.5f, 0.5f);
            markerLayer = new ItemizedLayer(mMap, new
                    ArrayList<MarkerItem>(),
                    symbol, handler);
            mMap.layers().add(markerLayer);
            //绘制
            markerLayer.addItem(new MarkerItem("我的位置", "", new GeoPoint(39.896116, 116.434065)));
            mMap.render();
        }
        MapPosition mapPosition = mMap.getMapPosition();
        mMap.animator().animateTo(1500, new MapPosition(39.896116, 116.434065, mapPosition.getScale()));
        //加圆
        float color[] = {0f, 0.05f, 0.15f, 0.03f};
        GCircleOverlay mLocationOverlay = new GCircleOverlay(mMap, 39.896116, 116.434065, 100, color, true);
        mMap.layers().add(mLocationOverlay);
    }


    @Override
    public boolean onItemLongPress(int arg0, Object arg1) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onItemSingleTapUp(int arg0, Object obj) {
        MarkerItem item = (MarkerItem) obj;
        Toast toast = Toast.makeText(this, item.getTitle(), Toast.LENGTH_SHORT);
        toast.show();
        return false;
    }


    //点击点弹出信息框
    class POIInfoWindow extends DefaultInfoWindow {

        private Button mButton;
        private ImageView mImage;

        public POIInfoWindow(Map map) {
            super(MainActivity.this, R.layout.bonuspack_bubble, mMapView);

            mButton = (Button) mView.findViewById(R.id.bubble_moreinfo);
            mImage = (ImageView) mView.findViewById(R.id.bubble_image);

            //bonuspack_bubble layouts already contain a "more info" button.
            mButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    POI poi = (POI) view.getTag();

                    if (poi == null)
                        return;

					/*if (poi.serviceId == POI.POI_SERVICE_4SQUARE) {
                        FourSquareProvider.browse(view.getContext(), poi);
					} else*/
                    if (poi.url != null) {
                        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(poi.url));
                        i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                        view.getContext().startActivity(i);
                    }
                }
            });

            getView().setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    POI poi = (POI) view.getTag();

                    //if (poi != null)
                    //	showPOIActivity(false);
                }
            });
        }

        @Override
        public void onOpen(ExtendedMarkerItem item) {
            POI poi = (POI) item.getRelatedObject();

            super.onOpen(item);

            poi.fetchThumbnail(mImage);

            //Show or hide "more info" button:
            if (poi.url != null)
                mButton.setVisibility(View.VISIBLE);
            else
                mButton.setVisibility(View.GONE);

            mButton.setTag(poi);
            getView().setTag(poi);
        }
    }
}


